import Joi from 'joi';

import { productValidator, simModelValidator, timeoutValidator, specifiedLocalPathValidator } from './customValidator';

const allowedParamTypes = ['number', 'string', 'array', 'file'];
const allowedModelLocationTypes = ['local', 'remote'];
export class Schema {
  static jobRequestSchema = Joi.object().keys({
    id: Joi.string().required(),
    context: Joi.object({
      userId: Joi.string().required(),
      appId: Joi.string().required(),
      nodeId: Joi.string().required(),
      debug: Joi.bool().default(true)
    }).required(),
    product: Joi.string().required().custom(productValidator),
    definition: {
      model: {
        file: Joi.string()
          .required()
          .when('type', { is: Joi.valid('local'), then: Joi.custom(simModelValidator) }),
        type: Joi.string().allow(...allowedModelLocationTypes).default('remote'),
        entry: Joi.string().allow('')
      },
      parameters: Joi.array().items({
        type: Joi.any().allow(...allowedParamTypes),
        name: Joi.string().required(),
        value: Joi.any().required(),
        unit: Joi.string().allow('')
      }),
      result: Joi.object().keys({
        specifiedLocalPath: Joi.string().allow('').custom(specifiedLocalPathValidator),
        uploadRawSimResult: Joi.boolean(),
        specifiedOutputParams: Joi.any(),
        extendedProperties: Joi.object()
      }),

      config: Joi.object().keys({
        timeout: Joi.number().custom(timeoutValidator)
      })
    }
  });
}